package blogs.eidos.cloud.framework.core.threadlocal;

import net.bigdata.cloud.framework.common.api.HttpCodeEnum;
import net.bigdata.cloud.framework.shiro.vo.LoginUser;
import org.apache.shiro.authc.AuthenticationException;

/**
 * @author ： Eidos
 * @date ：Created in 2021/4/12
 * @description：
 * @modified By：
 * @version: 1.0
 */
public class LoginUserThreadLocal<T> {

    private static ThreadLocal<LoginUser> userThreadLocal = new ThreadLocal<>();

    public static void setLoginUser(LoginUser loginUser) {
        if (null != loginUser) {
            userThreadLocal.set(loginUser);
        } else {
            throw new AuthenticationException(HttpCodeEnum.ACCESS_DENIED_ERROR.getMessage());
        }
    }

    public static LoginUser getLoginUser() {
        LoginUser loginUser = userThreadLocal.get();
        if (null != loginUser) {
            return loginUser;
        } else {
            throw new AuthenticationException(HttpCodeEnum.ACCESS_DENIED_ERROR.getMessage());
        }
    }

    public static void clear() {
        userThreadLocal.remove();
    }


}
